# -*- coding: utf-8 -*-

import numpy as np
import pandas as pd
import lightgbm as lgb
import scipy as sp

def logloss(act, pred):
  epsilon = 1e-15
  pred = sp.maximum(epsilon, pred)
  pred = sp.minimum(1-epsilon, pred)
  ll = sum(act*sp.log(pred) + sp.subtract(1,act)*sp.log(sp.subtract(1,pred)))
  ll = ll * -1.0/len(act)
  return ll

def loadCSV(path=''):
    return pd.read_csv(path)
   
    
#训练分类器XGB
def LGBM(x_train,y_train):
    
    print('使用LIGHTBGM进行训练')
    
    lgb_train = lgb.Dataset(x_train, y_train)

    params = {
        'task': 'train',
        'boosting_type': 'gbdt',
        'objective': 'binary',
        'metric': 'binary_logloss',
        'num_leaves': 360, 
        'learning_rate': 0.02,
        'feature_fraction': 0.5,
        'verbose': 0,
    }
  
    lgbm = lgb.train(params,
                lgb_train,
                num_boost_round=700)
        
    return lgbm
    
def predict_test_prob(lgbm):
    df_all=loadCSV('data/cutData/test_v6.csv') 
    
    df_sta_xgb=loadCSV('data/pro/pro_xgb_test.csv') 
    print('开始拼接')
    df_all=pd.merge(df_all,df_sta_xgb,how='left',on='instanceID')
    del df_sta_xgb   
    instanceID=df_all.instanceID.values
    feature_all=df_all.drop(['label','clickTime','instanceID','sort',
                             'appCategory'],axis=1).values
                             
    prob = lgbm.predict(feature_all, num_iteration=lgbm.best_iteration)

    output=pd.DataFrame({'instanceID':instanceID,'prob':prob})
    
    output.to_csv('result/submission.csv',index=False) 
    
def pointReserve():
    df_point=loadCSV('result/submission.csv')
    df_point['prob']=df_point.prob.apply(lambda x:round(x,10))
    df_point.to_csv('result/submission2.csv',index=False) 
    
#主函数入口
if __name__=='__main__':
    
   df_all=loadCSV('data/cutData/feature_v6.csv')
   df_sta_xgb=loadCSV('data/pro/pro_xgb_train.csv')
   
   print('开始拼接')
   df_all=pd.merge(df_all,df_sta_xgb,how='left',on='sort')
   del df_sta_xgb
   
   print('开始训练')
   
   feature_all=df_all.drop(['sort','label','clickTime','conversionTime',
                            'appCategory'],axis=1).values
   label_all=df_all.label.values
   
   del df_all
   
   bst=LGBM(feature_all,label_all)
   print('分类器训练完成')
   predict_test_prob(bst)
   print('结果预测完成')
